package com.sky.his.api.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.his.api.exception.HisException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import java.net.URLEncoder;
import java.util.HashMap;


@Component
@Slf4j
public class OcrUtil {

    @Value("${huawei.appKey}")
    private String appKey;

    @Value("${huawei.appSecret}")
    private String appSecret;

    public HashMap<String, Object> identifyWaybill(String imgBase64) throws Exception {
//        Request request = new Request();
//        request.setKey(appKey);
//        request.setSecret(appSecret);
//        request.setMethod("POST");  // HTTP method should be "POST"
//        request.setUrl("https://jmexpressbill.apistore.huaweicloud.com/ocr/expressbill");
//        request.addHeader("Content-Type", "application/x-www-form-urlencoded");
//
//        String encode = URLEncoder.encode(imgBase64, "UTF-8");  // Encoding with UTF-8
//
//        // Set the body with the encoded image
//        request.setBody("base64=" + encode);
//        HttpRequestBase signedRequest = Client.sign(request);
        // Create HTTP client object
        CloseableHttpClient client = HttpClients.custom().build();

        //TODO
        HttpRequestBase signedRequest = null;
        // Execute the request and get the response
        CloseableHttpResponse response = client.execute(signedRequest);
        if (response.getStatusLine().getStatusCode() != 200) {
            log.error("OCR识别运单失败", response.toString());
            throw new HisException("OCR识别运单失败");
        }
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            String responseString = EntityUtils.toString(entity, "UTF-8");

            JSONObject json = JSON.parseObject(responseString);

            int code = json.getIntValue("code");
            String msg = json.getString("msg");

            if (code == 200) {
                JSONObject data = json.getJSONObject("data");

                String recName = data.getString("recipient_name");
                String recTel = data.getString("recipient_phone");
                String waybillCode = data.getString("waybill_number");

                HashMap<String, Object> map = new HashMap<>();
                map.put("recName", recName);
                map.put("recTel", recTel);
                map.put("waybillCode", waybillCode);

                return map;
            } else {
                log.error("OCR运单识别失败: {}", msg);
                throw new HisException("OCR识别运单失败: " + msg);
            }
        } else {
            throw new HisException("OCR请求的响应体为空");
        }
    }
}